package github.kawaiior.yuanchuxiuzhen.network.packet;

import github.kawaiior.yuanchuxiuzhen.common.capability.entity.ManaPower;
import github.kawaiior.yuanchuxiuzhen.common.gongfa.base.BaiCaoZhenJing;
import github.kawaiior.yuanchuxiuzhen.core.registry.ModCapability;
import github.kawaiior.yuanchuxiuzhen.network.NetworkRegistryHandler;
import net.minecraft.entity.player.ServerPlayerEntity;
import net.minecraft.network.PacketBuffer;
import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.api.distmarker.OnlyIn;
import net.minecraftforge.common.util.LazyOptional;
import net.minecraftforge.fml.network.NetworkEvent;
import net.minecraftforge.fml.network.PacketDistributor;

import java.util.function.Supplier;

public class PlayerLeftClickPacket {

    public static void encode(PlayerLeftClickPacket packet, PacketBuffer buffer) {

    }

    public static PlayerLeftClickPacket decode(PacketBuffer buffer){
        return new PlayerLeftClickPacket();
    }

    public static void handlePacket(PlayerLeftClickPacket packet, Supplier<NetworkEvent.Context> content){
        NetworkEvent.Context context = content.get();
        context.enqueueWork(()->{
            if (context.getDirection().getReceptionSide().isClient()) {
                onClientCustomPack(packet, context);
            }
            if (context.getDirection().getReceptionSide().isServer()){
                onServerCustomPack(packet, context);
            }
        });
        context.setPacketHandled(true);
    }

    @OnlyIn(Dist.CLIENT)
    public static void onClientCustomPack(PlayerLeftClickPacket packet, NetworkEvent.Context context){

    }

    public static void onServerCustomPack(PlayerLeftClickPacket packet, NetworkEvent.Context context){
        ServerPlayerEntity player = context.getSender();
        assert player != null;
        LazyOptional<ManaPower> capability = player.getCapability(ModCapability.MANA_POWER);
        capability.ifPresent((power)->{
            BaiCaoZhenJing.onPlayerLeftClick(player, player.world, power);
        });
    }
}
